package com.fruit.resolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import com.fruit.controller.base.BaseController;

@ControllerAdvice
public class MyExceptionHandel implements HandlerExceptionResolver{

	protected Logger logger = LoggerFactory.getLogger(MyExceptionHandel.class);
	@Override
	@ExceptionHandler
	public ModelAndView resolveException(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex) {
		
		String returnUrl = "exception/definite";
		
		if(ex instanceof DefiniteException){
			ModelAndView mv =new ModelAndView(returnUrl);
			mv.addObject("message", ex.getMessage());
			this.logError(logger, ex.getMessage());
			return mv;
		}else{
			ModelAndView mv =new ModelAndView(returnUrl);
			mv.addObject("message", "抱歉，您请求的页面没有找到哦！");
			return mv;
		}
		
	}
	
	public void logBefore(Logger logger, String interfaceName){
		logger.info("");
		logger.info("start");
		logger.info(interfaceName);
	}
	
	public void logInfo(Logger logger, String interfaceName){
		logger.info("");
		logger.info("info");
		logger.info(interfaceName);
	}
	
	public void logWarn(Logger logger, String interfaceName){
		logger.warn("");
		logger.warn("warn");
		logger.warn(interfaceName);
	}
	
	public void logError(Logger logger, String interfaceName){
		logger.error("");
		logger.error("error");
		logger.error(interfaceName);
	}
	
	public void logAfter(Logger logger){
		logger.info("end");
		logger.info("");
	}	

}
